﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WorkStation.Frameworks.Core.Extensions;
using WorkStation.Frameworks.EasyBus.Attributes;

namespace WorkStation.Frameworks.EasyBus.Extensions
{
    public static class SubscribeAttributeExtensions
    {
        public static string GetMq(this SubscribeAttribute sub, IBus bus)
        {
            if (sub.Mq.StartsWith("Config:"))
            {
                string name = sub.Mq.Replace("Config:", "");

                object temp = null;

                if (bus.DataBag.TryGetValue("EasyBus.Endpoints", out temp))
                {
                    Dictionary<string, string> endpoints = temp as Dictionary<string, string>;

                    string endpointString = null;

                    if (endpoints.TryGetValue(name, out endpointString))
                    {
                        return endpointString;
                    }
                    else
                    {
                        throw new EasyBusException("EndpointName:{0}在配置文件中没有找到".Fmt(name));
                    }
                }
                else
                {
                    throw new EasyBusException("没有发现配置文件提供器提供的终结点数据，请把配置文件提供器放在属性配置提供器之前");
                }
            }
            else
            {
                return sub.Mq;
            }
        }
    }
}
